package com.hjwoo.app.views;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.hjwoo.app.R;
import com.hjwoo.app.baseviews.BaseAppCompatActivity;
import com.hjwoo.app.been.FailureResultObject;
import com.hjwoo.app.net.RequestListener;
import com.hjwoo.app.net.RequestTag;
import com.hjwoo.app.utils.LoanAccessTokenIdKeeper;
import com.hjwoo.app.utils.LoanLog;
import com.hjwoo.app.utils.LoanToast;
import com.hjwoo.app.utils.Utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by yangchuan on 2016/5/10.
 * 借款人信息界面
 */
public class CustomsDeclarationActivity extends BaseAppCompatActivity implements RequestListener {
    public static final int REQUEST_CODE_01 = 1;
    public static final int REQUEST_CODE_02 = 2;
    //获取Activity实例
    public static CustomsDeclarationActivity instance;
    EditText fullName, idNumber, spouseName, spouseIdNumber, spouseCellphone, cellphone, mortgagorFullName, mortgagorIdNumber, mortgagorSpouseFullName, mortgagorSpouseCellphone, mortgagorCellphone;
    TextView  mortgagorSpouseIdNumber, time;
    private LinearLayout borrower_conjoint1, mortgagor_conjoint1;
    Gson gson = new Gson();
    //    ["未婚", "已婚", "再婚", "离异", "丧偶"]
    String[] strs = new String[]{"未婚", "已婚", "再婚", "离异", "丧偶"};
    String pensonalisone, str1, str2, serialNumber, requestedAmount, loanDuration, mortgageType, firstMortgageAmount, typeOfFirstMortgage, secondMortgageAmount, attachment;
    HashMap op = new HashMap();//需要上传的参数
    TextWatcher watcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        }
        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        }
        @Override
        public void afterTextChanged(Editable editable) {
            int i = 0;
            if (cellphone.getText().toString().trim().length() > 0) {
                i++;
            }
            if (spouseCellphone.getText().toString().trim().length() > 0) {
                i++;
            }
            if (mortgagorCellphone.getText().toString().trim().length() > 0) {
                i++;
            }
            if (mortgagorSpouseCellphone.getText().toString().trim().length() > 0) {
                i++;
            }
            int result = 10 + (i * 5);
            time.setText("保护期时长：" + result + "天");
        }
    };

    //保护期动态显示
    private void setTime() {
        cellphone = (EditText) findViewById(R.id.cellphone_et);//借款人手机号
        spouseCellphone = (EditText) findViewById(R.id.spouseCellphone_et);//借款人配偶手机号
        mortgagorCellphone = (EditText) findViewById(R.id.mortgagorCellphone_et);//抵押
        mortgagorSpouseCellphone = (EditText) findViewById(R.id.mortgagorconjiontphone_et);//抵押人配偶手机号
        borrower_conjoint1 = (LinearLayout) findViewById(R.id.borrower_conjoint);
        mortgagor_conjoint1 = (LinearLayout) findViewById(R.id.mortgagor_conjoint);
        cellphone.addTextChangedListener(watcher);
        spouseCellphone.addTextChangedListener(watcher);
        mortgagorCellphone.addTextChangedListener(watcher);
        mortgagorSpouseCellphone.addTextChangedListener(watcher);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_customsdeclaration);
        instance = this;
        Utils.initTitle("立即报单", CustomsDeclarationActivity.this, true);
        setTime();
        if (getIntent() != null) {
            Intent intent = getIntent();
            pensonalisone = intent.getStringExtra("status");
            str1 = intent.getStringExtra("start1");
            str2 = intent.getStringExtra("start2");
            LoanLog.e("test", str1);
            LoanLog.e("test", str2);
            serialNumber = intent.getStringExtra("serialNumber");
            requestedAmount = intent.getStringExtra("requestedAmount");
            loanDuration = intent.getStringExtra("loanDuration");
            mortgageType = intent.getStringExtra("mortgageType");
            firstMortgageAmount = intent.getStringExtra("firstMortgageAmount");
            typeOfFirstMortgage = intent.getStringExtra("typeOfFirstMortgage");
            secondMortgageAmount = intent.getStringExtra("secondMortgageAmount");
            attachment = intent.getStringExtra("attachment");
            if (pensonalisone.equals("是")) {
                findViewById(R.id.activity_customsdeclaration_child01).setVisibility(View.GONE);
                if (str1.equals("已婚") || str1.equals("再婚")) {
                    borrower_conjoint1.setVisibility(View.VISIBLE);
                } else {
                    borrower_conjoint1.setVisibility(View.GONE);
                }
            } else if (pensonalisone.equals("否")) {
                findViewById(R.id.activity_customsdeclaration_child01).setVisibility(View.VISIBLE);
                if (str1.equals("已婚") || str1.equals("再婚")) {
                    borrower_conjoint1.setVisibility(View.VISIBLE);
                } else {
                    borrower_conjoint1.setVisibility(View.GONE);
                }
                if (str2.equals("已婚") || str2.equals("再婚")) {
                    mortgagor_conjoint1.setVisibility(View.VISIBLE);

                } else {
                    mortgagor_conjoint1.setVisibility(View.GONE);
                }
            }
      }
        time = (TextView) findViewById(R.id.customsdeclaration_time_txt);
        fullName = (EditText) findViewById(R.id.fullName_et);
        idNumber = (EditText) findViewById(R.id.idNumber_et);
        spouseName = (EditText) findViewById(R.id.spouseName_et);
        spouseIdNumber = (EditText) findViewById(R.id.spouseIdNumber_et);
        mortgagorFullName = (EditText) findViewById(R.id.mortgagorFullName_et);
        mortgagorIdNumber = (EditText) findViewById(R.id.mortgagorIdNumber_et);
        mortgagorSpouseIdNumber = (TextView) findViewById(R.id.mortgagorconjiontNumber_et);
        spouseCellphone = (EditText) findViewById(R.id.spouseCellphone_et);//借款人配偶手机号
        cellphone = (EditText) findViewById(R.id.cellphone_et);//借款人手机号
        mortgagorSpouseFullName = (EditText) findViewById(R.id.mortgagorconjiontname_et);//抵押人配偶手机号
//        mortgagorSpouseCellphone = (EditText) findViewById(R.id.mortgagorSpouseCellphone_et);//抵押人配偶手机号
        mortgagorCellphone = (EditText) findViewById(R.id.mortgagorCellphone_et);//抵押人手机号
//        spouse_ll = (LinearLayout) findViewById(R.id.spouse_ll);//借款人配偶
//        mortgagorspouse_ll = (LinearLayout) findViewById(R.id.mortgagorspouse_ll);//抵押人配偶

        findViewById(R.id.next).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                HashMap hashMap = new HashMap();
                hashMap.put("serialNumber", serialNumber);
                hashMap.put("requestedAmount", requestedAmount);
                hashMap.put("loanDuration", loanDuration);
                hashMap.put("mortgageType", mortgageType);
                hashMap.put("firstMortgageAmount", firstMortgageAmount);
                hashMap.put("typeOfFirstMortgage", typeOfFirstMortgage);
                hashMap.put("secondMortgageAmount", secondMortgageAmount);
                hashMap.put("cellphone", cellphone.getText().toString());


                if (fullName.getText().toString().equals("")) {
                    LoanToast.showToastShort("借款人姓名没填", CustomsDeclarationActivity.this);
                    return;
                }
                else {
                    hashMap.put("fullName", fullName.getText().toString());
                    hashMap.put("idNumber", idNumber.getText().toString());
                    hashMap.put("maritalStatus", str1);
                    if (!TextUtils.isEmpty(cellphone.getText().toString()) && !Utils.isMobileNO(cellphone.getText().toString())) {
                        //借款人手机号
                        LoanToast.showToastShort("借款人手机号格式有误", CustomsDeclarationActivity.this);
                        return;
                    }
                    if (!Utils.IDCardValidate(idNumber.getText().toString())) {
                        //借款人身份证号
                        LoanToast.showToastShort("借款人身份证号格式有误", CustomsDeclarationActivity.this);
                        return;
                    }
//                    if (borrower_conjoint1.getVisibility() == View.VISIBLE)
                    if (str1.equals("已婚") || str1.equals("再婚")) {
//                        当借款人有配偶时

                        if (TextUtils.isEmpty(spouseName.getText().toString())
                                ) {
                            LoanToast.showToastShort("借款人配偶姓名没填", CustomsDeclarationActivity.this);
                            return;
                        }
                        if (!TextUtils.isEmpty(spouseCellphone.getText().toString()) && !Utils.isMobileNO(spouseCellphone.getText().toString())) {
                            //借款人配偶手机号
                            LoanToast.showToastShort("借款人配偶手机号格式有误", CustomsDeclarationActivity.this);
                            return;
                        }
                        if (!Utils.IDCardValidate(spouseIdNumber.getText().toString())) {
                            //借款人配偶身份证号
                            LoanToast.showToastShort("借款人配偶身份证号格式有误", CustomsDeclarationActivity.this);
                            return;
                        }
                    }

                    List<HashMap> hms = new ArrayList<HashMap>();
                    if (borrower_conjoint1.getVisibility() == View.VISIBLE) {
//                贷款人
                        HashMap hm = new HashMap();
                        hm.put("idNumber", spouseIdNumber.getText().toString());
                        hm.put("cellphone", spouseCellphone.getText().toString());
                        hm.put("fullName", spouseName.getText().toString());
                        hm.put("type", "借款人配偶");
                        hms.add(hm);
                    }

                    if (!pensonalisone.equals("是")) {
                        LoanLog.e("cus", "jj");
                        if (mortgagorFullName.getText().toString().equals("")
                                ) {
                            LoanToast.showToastShort("抵押人姓名没填", CustomsDeclarationActivity.this);
                            return;
                        }
                        //抵押人
                        if (!TextUtils.isEmpty(mortgagorCellphone.getText().toString()) && !Utils.isMobileNO(mortgagorCellphone.getText().toString())) {
                            //抵押人手机号
                            LoanToast.showToastShort("抵押人手机号格式有误", CustomsDeclarationActivity.this);
                            return;
                        }
                        if (!Utils.IDCardValidate(mortgagorIdNumber.getText().toString())) {
                            //抵押人身份证号
                            LoanToast.showToastShort("抵押人身份证号格式有误", CustomsDeclarationActivity.this);
                            return;
                        }

                        if (mortgagor_conjoint1.getVisibility() == View.VISIBLE) {
                            LoanLog.e("cus", "mortgagorspouse_ll");
//                        当抵押人有配偶时
                            //抵押人配偶
                            if (TextUtils.isEmpty(mortgagorSpouseFullName.getText().toString())
                                    ) {
                                LoanToast.showToastShort("抵押人配偶姓名没填", CustomsDeclarationActivity.this);
                                return;
                            }
                            if (!TextUtils.isEmpty(mortgagorSpouseCellphone.getText().toString()) && !Utils.isMobileNO(mortgagorSpouseCellphone.getText().toString())) {
                                //抵押人配偶手机号
                                LoanToast.showToastShort("抵押人配偶手机号格式有误", CustomsDeclarationActivity.this);
                                return;
                            }
                            if (!Utils.IDCardValidate(mortgagorSpouseIdNumber.getText().toString())) {
                                //抵押人配偶身份证号
                                LoanToast.showToastShort("抵押人配偶身份证号格式有误", CustomsDeclarationActivity.this);
                                return;
                            }
                        }
                        //如果抵押人和贷款不一致传值
//                          加入 抵押人
                        HashMap hm2 = new HashMap();
                        hm2.put("idNumber", mortgagorIdNumber.getText().toString());
                        hm2.put("cellphone", mortgagorCellphone.getText().toString());
                        hm2.put("fullName", mortgagorFullName.getText().toString());
                        hm2.put("mortgagorMaritalStatus", str1);
                        hm2.put("type", "抵押人");
                        hms.add(hm2);

                        LoanLog.e("cus", hms.toString());

                        if (mortgagor_conjoint1.getVisibility() == View.VISIBLE) {
//                          加入 抵押人配偶
                            HashMap hm3 = new HashMap();
                            hm3.put("idNumber", mortgagorSpouseIdNumber.getText().toString());
                            hm3.put("cellphone", mortgagorSpouseCellphone.getText().toString());
                            hm3.put("fullName", mortgagorSpouseFullName.getText().toString());
                            hm3.put("type", "抵押人配偶");
                            hms.add(hm3);
                        }
                    }
                    hashMap.put("contacts", hms);
                }

                op.put("opportunity", hashMap);
                op.put("sessionId", LoanAccessTokenIdKeeper.readAccessToken(CustomsDeclarationActivity.this).getAppSessionId());
//                RequestMethod.getMethod().appProjectTime(LoanAccessTokenIdKeeper.readAccessToken(CustomsDeclarationActivity.this).getAppSessionId(),idNumber.getText().toString(), CustomsDeclarationActivity.this, CustomsDeclarationActivity.this);//这里调用接口请求网络
            }
        });

    }

    @Override
    public void requestSuccess(String tag, String message) {
        LoanLog.e("dd","dd");
        if (tag.equals(RequestTag.APPPROJECTTIME)) { //使用tag来辨别消息分发
            Intent intent1 = new Intent(CustomsDeclarationActivity.this, UpLoadMaterialActivity.class);
            intent1.putExtra("starus1", pensonalisone);
            intent1.putExtra("gam", str1);
            intent1.putExtra("gam1", str2);
            intent1.putExtra("serialnumber", serialNumber);
            intent1.putExtra("submitparms", gson.toJson(op));
            startActivity(intent1);
        }
    }

    @Override
    public void requestError(String message) {
        LoanLog.e("cus", message);
        FailureResultObject failureResultObject = new FailureResultObject();
        failureResultObject = gson.fromJson(message, FailureResultObject.class);
        LoanToast.showToastShort(failureResultObject.getErrorMessage(), CustomsDeclarationActivity.this);
    }
}
